using Microsoft.Extensions.DependencyInjection;
using ZeroSum.DependencyInjection.Attributes;
using ZeroSum.Extend.Mapper.Extensions;
using ZeroSumZSA.Application.ApplicationServices.Menus.Dto.In;
using ZeroSumZSA.Application.ApplicationServices.Menus.Dto.Out;
using ZeroSumZSA.Application.ApplicationServices.Menus.Dto.Query;
using ZeroSumZSA.Core.Aggregates.MenuAggregate;
using ZeroSumZSA.Core.DomainServices.Menus;

namespace ZeroSumZSA.Application.ApplicationServices.Menus;

[Register(ServiceLifetime.Scoped)]
public class MenuApplicationService : IMenuApplicationService
{
    private readonly IMenuDomainService _domainService;

    public MenuApplicationService(IMenuDomainService domainService)
    {
        _domainService = domainService;
    }

    public async Task Add(AddMenuDto dto)
    {
        var entity = dto.MapTo<Menu>();
        await _domainService.AddAsync(entity);
    }

    public async Task Delete(DeleteMenuDto input)
    {
        await _domainService.DeleteAsync(input.Id);
    }

    public async Task UpdateAsync(EditMenuDto dto)
    {
        await _domainService.CheckRoleUpdate(dto.Id);
        var role = await _domainService.GetByIdAsync(dto.Id);
        var entity = dto.MapTo(role);
        await _domainService.UpdateAsync(entity);
    }

    public async Task<DetailMenuDto> Detail(DetailMenuQuery query)
    {
        var role = await _domainService.GetByIdAsync(query.Id);
        return role.MapTo<DetailMenuDto>();
    }
}